﻿namespace KeRui.TemplateMaker
{
    using KeRui.BLL;
    using KeRui.Model;
    using iPortal.TemplateEngine;
    using System;
    using System.Text;
    using System.Web;

    public class TemplateBase
    {
        public TemplateDocument GetTemplate(PublicTypeEnum.PublicType publicType, int dataID)
        {
            switch (publicType)
            {
                case PublicTypeEnum.PublicType.Index:
                {
                    KeRui.Model.CmsSite modelByCache = new KeRui.BLL.CmsSite().GetModelByCache(dataID);
                    return LoadTemplate(modelByCache.TemplateDir + @"\" + modelByCache.TemplateFolder + @"\Index.shtml");
                }
                case PublicTypeEnum.PublicType.Node:
                {
                    KeRui.BLL.CmsNode node = new KeRui.BLL.CmsNode();
                    return LoadTemplate(node.GetModelByCache(dataID).TemplateFile);
                }
                case PublicTypeEnum.PublicType.Content:
                {
                    KeRui.BLL.CmsSite site3 = new KeRui.BLL.CmsSite();
                    KeRui.BLL.CmsNode node3 = new KeRui.BLL.CmsNode();
                    string templateFolder = site3.GetModelByCache(node3.GetModelByCache(dataID).SiteID).TemplateFolder;
                    KeRui.BLL.CmsContent content = new KeRui.BLL.CmsContent();
                    return LoadTemplate(HttpContext.Current.Server.MapPath("/Template/" + templateFolder + "/" + content.GetModel(dataID).TemplateFile));
                }
            }
            return null;
        }

        private static TemplateDocument LoadTemplate(string filePath)
        {
            return new TemplateDocument(filePath, Encoding.GetEncoding("utf-8"));
        }
    }
}

